package com.demo.cms.servlet;

import java.awt.List;
import java.io.IOException;
import java.io.Writer;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import freemarker.template.Configuration;

/**
 * 文档管理Servlet
 * @author ryuu.kk
 *
 */
public class DocumentManageServlet extends HttpServlet {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 2891508414064832212L;

	/**
	 * FreemarkerConfig
	 */
    private Configuration configuration;  
    
	public void init() throws ServletException {
		configuration = new Configuration();   
        configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/pages");   
        configuration.setEncoding(Locale.CHINA, "UTF-8");   
    }
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "/WEB-INF/fileManager.jsp";
		
		Cookie[] cookies = request.getCookies();
		String keyOrder = "KCFINDER_order";
		String keyView = "KCFINDER_view";
		//默认Order by的name被选中
		Cookie cookie_order = new Cookie(keyOrder, "name");
		Cookie cookie_view = new Cookie(keyView, "thumbs");
		if (cookies!= null && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (keyOrder.equals(c.getName())) {
					cookie_order = c;
				}
				if (keyView.equals(c.getName())) {
					cookie_view = c;
				}
			}
		}
		
		String action = request.getParameter("act");
		ObjectMapper mapper = new ObjectMapper();
		String result = "{\"tree\":{\"name\":\"images\",\"readable\":true,\"writable\":false,\"removable\":false,\"hasDirs\":true,\"dirs\":[{\"name\":\"Photos from Bulgaria\",\"readable\":true,\"writable\":false,\"removable\":false,\"hasDirs\":false},{\"name\":\"public\",\"readable\":true,\"writable\":true,\"removable\":false,\"hasDirs\":false,\"current\":true, \"dirs\":[{\"name\":\"123\",\"readable\":true,\"writable\":true,\"removable\":true,\"hasDirs\":false}]},{\"name\":\"Sonisphere - Sofia 2010\",\"readable\":true,\"writable\":false,\"removable\":false,\"hasDirs\":false}]},\"files\":[{\"name\":\"Blue hills(1).jpg\",\"size\":28521,\"mtime\":1333089452,\"date\":\"03/30/2012 06:37 AM\",\"readable\":true,\"writable\":true,\"bigIcon\":true,\"smallIcon\":true,\"thumb\":true,\"smallThumb\":false},{\"name\":\"Blue hills.jpg\",\"size\":28521,\"mtime\":1333089423,\"date\":\"03/30/2012 06:37 AM\",\"readable\":true,\"writable\":true,\"bigIcon\":true,\"smallIcon\":true,\"thumb\":true,\"smallThumb\":false},{\"name\":\"close_button.png\",\"size\":858,\"mtime\":1333086221,\"date\":\"03/30/2012 05:43 AM\",\"readable\":true,\"writable\":true,\"bigIcon\":true,\"smallIcon\":true,\"thumb\":false,\"smallThumb\":true}],\"dirWritable\":true}";
		JsonNode node = mapper.readTree(result);
		if ("init".equals(action)) {
			Writer writer = response.getWriter();
			
			writer.write(result);
			writer.flush();
			writer.close();
			return;
		}
		
		response.addCookie(cookie_order);
		response.addCookie(cookie_view);
		request.getRequestDispatcher(url).forward(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
